package com.mc.cloud.mall.categoryProduct.controller;

import com.github.pagehelper.PageInfo;
import com.mc.cloud.mall.categoryProduct.model.pojo.Product;
import com.mc.cloud.mall.categoryProduct.model.vo.AddProduct;
import com.mc.cloud.mall.categoryProduct.model.vo.UpdateProduct;
import com.mc.cloud.mall.categoryProduct.service.AdminProductService;
import com.mc.cloud.mall.common.common.MallRestResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @ClassName AdminProductController
 * @Author MC
 * @Date 2020/11/5 9:34
 * @Version 1.0
 **/
@RestController
public class AdminProductController {
    
    @Autowired
    AdminProductService adminProductService;
    
    /**
     * 添加商品
     * @param addProduct
     * @return
     */
    @ApiOperation("添加商品")
    @PostMapping("admin/product/add")
    public MallRestResponse addProduct(@Valid @RequestBody AddProduct addProduct){
        
        adminProductService.addProduct(addProduct);
        return MallRestResponse.success();
    }
    
    /**
     * 上传图片
     * @param request
     * @param file
     * @return
     */
    @ApiOperation("上传图片")
    @PostMapping("admin/upload/file")
    public MallRestResponse upload(HttpServletRequest request, MultipartFile file){
        return adminProductService.upload(request,file);
    }
    
    /**
     * 更新商品
     * @param updateProduct
     * @return
     */
    @ApiOperation("更新商品")
    @PostMapping("/admin/product/update")
    public MallRestResponse updateProduct(@Valid @RequestBody UpdateProduct updateProduct){
    
        Product product = new Product();
        BeanUtils.copyProperties(updateProduct,product);
        adminProductService.updateProduct(product);
        return MallRestResponse.success();
        
    }
    
    /**
     * 删除商品
     * @param id
     * @return
     */
    @ApiOperation("删除商品")
    @PostMapping("/admin/product/delete")
    public MallRestResponse deleteProduct(@RequestParam Integer id){
        adminProductService.deleteProduct(id);
        return MallRestResponse.success();
    }
    
    
    /**
     * 批量上下架商品
     * @param ids
     * @param sellStatus
     * @return
     */
    @ApiOperation("批量上下架商品")
    @PostMapping("/admin/product/batchUpdateSellStatus")
    public MallRestResponse batchUpdateSellStatus(@RequestParam Integer[] ids,@RequestParam Integer sellStatus){
        
        adminProductService.batchUpdateSellStatus(ids,sellStatus);
        return MallRestResponse.success();
    }
    
    /**
     * 商品列表
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation("商品列表")
    @GetMapping("/admin/product/list")
    public MallRestResponse productList(@RequestParam Integer pageSize,@RequestParam Integer pageNum){
        PageInfo pageInfo = adminProductService.productList(pageSize, pageNum);
        return MallRestResponse.success(pageInfo);
    
    }
    
}
